package com.demo.controller;
import java.util.Arrays;

import javax.servlet.http.HttpSession;

import com.demo.entity.Book;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;



@Controller
public class DemoController {

	@GetMapping("/toLogin")
	public String toLogin() {
//		if(true)
//		{
//			throw new RuntimeException();
//		}
		
		return "login";
	}

	@PostMapping("/login")
	public String toLogin(HttpSession session, String loginName,String password,Model model) {
		System.out.println(loginName);
		System.out.println(password);
		if(loginName.equals("admin") && password.equals("123456")) {
			session.setAttribute("user", loginName);
			return "redirect:/main";
		}else {
			model.addAttribute("errorMsg", "用户名或密码错误");
			return "login";
		}
	}

	@GetMapping("/main")
	public String main(Model model) {
		Book b1 = new Book("1001","疯狂Java讲义","李刚","电子工业出版社",108.00,"java.png");
		Book b2 = new Book("1001","疯狂Java讲义2","李刚","电子工业出版社",108.00,"java.png");
		Book b3 = new Book("1001","疯狂Java讲义3","李刚","电子工业出版社",108.00,"java.png");
		Book b4 = new Book("1001","疯狂Java讲义4","李刚","电子工业出版社",108.00,"java.png");
		Book b5 = new Book("1001","疯狂Java讲义5","李刚","电子工业出版社",108.00,"java.png");
		model.addAttribute("books", Arrays.asList(b1,b2,b3,b4,b5));
		return "main";
	}


}
